package com.yuan.cfycomment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.global.Constant;
import com.example.jhf.R;
import com.imooc.baseadapter.utils.Tools;

import android.R.bool;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 闁告帗绋戠紓鎾诲籍閵夛附鍩�: 2015/10/16 闁告帗绋戠紓鎾绘嚀閸滃啰绐� 闁告帗蓱濡楀牓宕￠敓锟�
 * 闁告梻鍠曢崗妯兼嫚鐎涙ɑ顫栭柨娑欒壘瑜板倻鎮伴妸銉
 * 晙闂傦拷鐠恒劍娈岄悷鍑ゆ嫹闁伙絽鐭傚浼存晬瀹�锟介弫銈嗙鎼淬値鍞剁憸鐗堟礀椤斿秹姊归崹顔肩岛闂侇偂鑳跺▓鎴︽偩濞嗘巻鏋�
 * 濞ｅ浂鍠楅弫鑲╀沪閵夈儱鍧婇柨娑虫嫹 VER 濞ｅ浂鍠楅弫濂稿籍閿燂拷 濞ｅ浂鍠楅弫濂告嚀閿燂拷 濞ｅ浂鍠楅弫濂稿礃閸涱収鍟囬柨娑樼箳閹﹪鎮介敓锟�
 * 闁崇櫢鎷烽柍鐧告嫹闁崇櫢鎷烽柍鐧告嫹闁崇櫢鎷烽柍鐧告嫹闁崇櫢鎷烽柍鐧告嫹闁崇櫢鎷烽柍鐧告嫹闁崇櫢鎷烽柍鐧告嫹闁崇櫢鎷烽柍鐧告嫹闁崇櫢鎷烽柍鐧告嫹闁崇櫢鎷烽柍鐧告嫹闁崇櫢鎷烽柍鐧告嫹闁崇櫢鎷烽柍鐧告嫹闁崇櫢鎷烽柍鐧告嫹闁崇櫢鎷烽柍鐧告嫹闁崇櫢鎷烽柍鐧告嫹闁崇櫢鎷烽柍鐧告嫹闁崇櫢鎷烽柍鐧告嫹闁崇櫢鎷烽柍鐧告嫹闁崇櫢鎷烽柍鐧告嫹闁崇櫢鎷烽柍鐧告嫹闁崇櫢鎷烽柍鐧告嫹闁崇櫢鎷烽柍鐧告嫹闁崇櫢鎷烽柍鐧告嫹闁崇櫢鎷烽柍鐧告嫹闁崇櫢鎷烽柍鐧告嫹闁崇櫢鎷烽柍鐧告嫹闁崇櫢鎷烽柍鐧告嫹闁崇櫢鎷烽柍鐧告嫹闁崇櫢鎷烽柍鐧告嫹闁崇櫢鎷烽柍鐧告嫹闁崇櫢鎷烽柍鐧告嫹闁崇櫢鎷烽柍鐧告嫹
 */
public class te extends Activity implements OnClickListener {
	private EditText editText;
	private TextView tvSend, tvCancel;
	public static HashMap<String, String> intn;
	public static String heheString;
	public static boolean Name = false;
	private ObjectMapper om = new ObjectMapper();
	private String code;
	private JSONObject jsonObject;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.te);
		SharedPreferences sp = getSharedPreferences("UserId", MODE_PRIVATE);
		String uid = sp.getString("uid", "0");
		tvCancel = (TextView) findViewById(R.id.tv_cancel_message);
		tvSend = (TextView) findViewById(R.id.tv_send_message);
		editText = (EditText) findViewById(R.id.et_meeage_content);
		tvCancel.setOnClickListener(this);
		tvSend.setOnClickListener(this);
		getText();

	}

	/**
	 * 闁兼儳鍢茬欢杈╂啺娴ｇ绲洪梺顐¤兌濞堟垿寮悧鍫濈ウ
	 */
	private void getText() {
		tvSend.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				heheString = editText.getText().toString();
				Name = true;
				getJsonText();
				//startActivity(new Intent(te.this, Leave_word.class));
				Intent intent = new Intent();
				intent.setAction("action.refreshLeaveMessage");
				sendBroadcast(intent);
				finish();
			}
		});
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.tv_cancel_message:
			finish();
			break;
		case R.id.tv_send_message:
			finish();
		default:
			break;
		}
	}

	/**
	 * 闁告瑦鍨块敓鎴掔椤斿秹姊圭捄銊︽畬閻熷嚖鎷�
	 */
	@SuppressLint("HandlerLeak")
	private void getJsonText() {
		final Handler handler = new Handler() {
			private JSONObject jsonObject2;
			private JSONObject jsonObjectSchool;
			private String schoolanswer;

			public void handleMessage(Message msg) {
				if (msg.what == 7) {
					String result = msg.obj.toString();
					try {
						JSONObject json = new JSONObject(result);
						JSONObject jsonObject = json.getJSONObject("data");

						String date = (String) jsonObject.get("date");
						// String student_class = (String)
						// jsonObject.get("student_class");
						// String student_name = (String)
						// jsonObject.get("student_name");
						String user = jsonObject.getString("user");
						String note = (String) jsonObject.get("note");
						String user_image = (String) jsonObject.get("user_image");
						Intent intent = new Intent();
						intent.putExtra("heheString", note);
						intent.putExtra("date_answer", date);
						// intent.putExtra("student_class_answer",
						// student_class);
						// intent.putExtra("student_name_answer", student_name);
						intent.putExtra("user", user);
						intent.putExtra("student_image_answer", user_image);
						intent.setAction("action.refreshLeavMessage");
						sendBroadcast(intent);
						// setResult(RESULT_OK, intent);
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} else if (msg.what == 6) {
					Toast.makeText(te.this, "闁告瑦鍨块敓鎴掔閵囨垹鎷归敓锟�", 0).show();
				} else if (msg.what == 10) {
					Toast.makeText(te.this, "闁告瑦鍨块敓鎴掔劍濠�渚�寮敓锟�", 0).show();
				}

			}

		};
		new Thread() {
			public void run() {
				SharedPreferences sp = getSharedPreferences("UserId", MODE_PRIVATE);
				String uid = sp.getString("uid", "0");
				List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>();
				params.add(new BasicNameValuePair("note", heheString));
				params.add(new BasicNameValuePair("uid", uid)); // iud
				params.add(new BasicNameValuePair("db", Constant.db));// 闁轰胶澧楀畵浣规償閿燂拷
				String result = Tools.sendPost(Constant.publishComment, params);
				Log.v("result", result + "婵炴潙顑堥惁锟�");
				Message message = new Message();
				try {
					if (result != null) {
						JsonNode jsonNode = om.readTree(result);
						code = jsonNode.get("code").asText();
						if (code.equals("200")) {
							message.what = 7;
							message.obj = result;
						} else {
							message.what = 6;// 闁告瑦鍨块敓鎴掔閵囨垹鎷归敓锟�
						}
					} else {
						message.what = 9;//
					}
					handler.sendMessage(message);
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
					message.what = 10;
					handler.sendMessage(message);
				}
			};
		}.start();
	}
}
